package com.wx.mall.operate.controller;


import com.wx.mall.common.api.CommonPage;
import com.wx.mall.common.api.CommonResult;
import com.wx.mall.common.api.ResultCode;
import com.wx.mall.common.exceptions.ApiAssert;
import com.wx.mall.operate.dto.DiscountDto;
import com.wx.mall.operate.entity.Discount;
import com.wx.mall.operate.service.IDiscountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 优惠券表 前端控制器
 * </p>
 *
 * @author j
 * @since 2020-05-13
 */
@RestController
@RequestMapping("/discount")
@Api(tags = "优惠券表",description = "DiscountController")
@Slf4j
public class DiscountController {

    @Resource
    private IDiscountService service;

    @ApiOperation("添加优惠券")
    @PostMapping("/addOne")
    public CommonResult addOne(Discount discount){
        boolean sucess=service.save(discount);

        return sucess? CommonResult.success(): CommonResult.failed();

    };

    @ApiOperation("根据优惠券名获取id")
    @GetMapping("/getId")
    public CommonResult<Integer> getId(String disName){
       Integer disId=service.getId(disName);
    return CommonResult.success(disId);
    };

    @ApiOperation("根据id获取数据")
    @GetMapping("/getById")
    public CommonResult<Discount> getById(Integer disId){
        Discount discount=service.getById(disId);
        return CommonResult.success(discount);
    };



    @ApiOperation("获取数据列表（带筛选和排序）")
    @GetMapping("/getList")
    public CommonResult<CommonPage<Discount>> getList(@ApiParam("获取dto数据") DiscountDto dto){
        System.out.println(dto);
        if (dto.getPageNum() == null || dto.getPageSize() == null) {
            ApiAssert.fail(ResultCode.VALIDATE_FAILED); // 直接抛异常，使用enum
        }
        if (dto.getPageNum() <= 0) {
            ApiAssert.fail("无效页码"); // 直接抛异常，使用自定义的message
        }

        List<Discount> list=service.getList(dto);

        return CommonResult.success(CommonPage.restPage(list));


    };



    @ApiOperation("修改优惠券")
    @PostMapping("/updateOne")
    public CommonResult updateOne(Discount discount){
        System.out.println(discount);
        boolean sucess=service.updateById(discount);

        return sucess? CommonResult.success(): CommonResult.failed();

    };




}

